package com.weiyin.examination.course;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.adapter.Bottom1Adapter;
import com.weiyin.examination.adapter.BottomAdapter;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.application.ResId;
import com.weiyin.examination.dialog.BottomDialog;
import com.weiyin.examination.dialog.FlatSharpDialog;
import com.weiyin.examination.http.RetrofitClient;
import com.weiyin.examination.listener.CourseInterface;
import com.weiyin.examination.listener.DialogResultInterface;
import com.weiyin.examination.listener.DialogShowListener;
import com.weiyin.examination.module.AnswerModule;
import com.weiyin.examination.module.AnswerXml;
import com.weiyin.examination.module.BottomModule;
import com.weiyin.examination.module.Note;
import com.weiyin.examination.module.TestModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;
import com.weiyin.examination.view.CourseView;
import com.weiyin.examination.view.GridLayoutManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author njf
 *         Created by lenovo on 2017/9/19.
 */

public class CourseA9 extends Course implements BottomAdapter.OnItemClickInterface, Bottom1Adapter.OnItemClickInterface, CourseView.CourseViewListener {
    private View rootView;
    private Activity activity;
    private ClickAble clickAble;

    private RecyclerView bottomRecyclerView1, bottomRecyclerView11;
    private Bottom1Adapter bottom1Adapter1;
    private BottomAdapter bottomAdapter1;
    private TextView titleLayoutNextTextView, titleLayoutLastTextView, titleLayoutSubjectContentTextView, titleLayoutSubjectScoreTextView,
            titleLayoutNumberTextView, titleTicketNumber, courseA9TextViewSubmit, courseA9TextViewNext, courseA9Rl4TextViewLast, courseA9Rl3TextViewNext, courseA9TextViewLast;
    private RelativeLayout titleLayoutNumberRl, courseA9Rl1, courseA9Rl2, courseA9Rl3, courseA9Rl4;
    private ImageView courseA9ImageView1, courseA9FlatSharp, courseA9TimeSignature, courseA9Rl2ImageView1,
            courseA9Rl3ImageView1;
    private View courseA9Rl2Include;

    private TestModule testModule;
    private List<TestModule> testModuleList;
    private List<AnswerModule> answerModuleList;
    private int courseIndex, viewIndex = 0;
    private Handler mHandler = new Handler();
    private CourseView courseView, courseView1, courseView2;
    private int bottomIndex = -1, riseDropIndex = -1;

    private List<BottomModule> integerList, integerList1;
    private List<String> stringList, stringList1, stringList2;
    private boolean recyclerViewClef = true;
    private boolean recyclerViewClick = true, recyclerViewSpot = false, recyclerViewHomoPhonicConnection = false, recyclerViewThreeNote = false, recyclerViewIsRepose = false;
    private View recyclerViewView, recyclerViewView1, recyclerViewView2, recyclerViewView3, recyclerViewView4;
    private int recyclerViewMark = 0, recyclerView1Mark = 0;
    private LayoutHelper layoutHelper;
    private ResId resId;

    private double score;


    @Override
    public void onCreate(Activity activity, ViewFlipper contain, List<TestModule> courseEntities, int index) {
        if (rootView == null) {
            rootView = LayoutInflater.from(activity).inflate(R.layout.coursea9_layout, null);
            layoutHelper = new LayoutHelper(activity);
            layoutHelper.scaleView(rootView);

            contain.addView(rootView);
            this.testModuleList = courseEntities;
            this.activity = activity;
            this.courseIndex = index;
            this.testModule = testModuleList.get(index);
            clickAble = new ClickAble();
            resId = new ResId();
            getImage();
            if (testModule != null) {
                if (testModule.getRightAnswerList() != null) {
                    this.answerModuleList = testModule.getRightAnswerList();
                }
            }
            initView();
        }
    }

    private void getImage() {
        if (integerList == null) {
            integerList = new ArrayList<>();
        }
        for (int i = 0; i < resId.getImageId().length; i++) {
            BottomModule bottomModule = new BottomModule();
            bottomModule.setBitmapId(resId.getImageId()[i]);
            bottomModule.setGreyBitmapId(resId.getGreyImageId()[i]);
            bottomModule.setClick(true);
            integerList.add(bottomModule);
        }
        if (integerList1 == null) {
            integerList1 = new ArrayList<>();
        }
        for (int i = 0; i < resId.getImageId().length; i++) {
            BottomModule bottomModule = new BottomModule();
            bottomModule.setBitmapId(resId.getImageId()[i]);
            bottomModule.setGreyBitmapId(resId.getGreyImageId()[i]);
            bottomModule.setClick(true);
            integerList1.add(bottomModule);
        }
        if (stringList == null) {
            stringList = new ArrayList<>();
        }
        Collections.addAll(stringList, resId.getStringId());
        if (stringList1 == null) {
            stringList1 = new ArrayList<>();
        }
        Collections.addAll(stringList1, resId.getStringId());

        if (stringList2 == null) {
            stringList2 = new ArrayList<>();
        }
        Collections.addAll(stringList2, resId.getStringId());
    }

    @Override
    public void initView() {
        courseA9Rl2Include = rootView.findViewById(R.id.courseA9Rl2Include);

        courseA9FlatSharp = (ImageView) rootView.findViewById(R.id.courseA9FlatSharp);
        courseA9TimeSignature = (ImageView) rootView.findViewById(R.id.courseA9TimeSignature);

        titleLayoutNextTextView = (TextView) rootView.findViewById(R.id.titleLayoutNextTextView);
        titleLayoutLastTextView = (TextView) rootView.findViewById(R.id.titleLayoutLastTextView);
        titleLayoutSubjectContentTextView = (TextView) rootView.findViewById(R.id.titleLayoutSubjectContentTextView);
        titleLayoutSubjectScoreTextView = (TextView) rootView.findViewById(R.id.titleLayoutSubjectScoreTextView);
        titleLayoutNumberTextView = (TextView) rootView.findViewById(R.id.titleLayoutNumberTextView);
        courseA9ImageView1 = (ImageView) rootView.findViewById(R.id.courseA9ImageView1);
        courseA9Rl2ImageView1 = (ImageView) rootView.findViewById(R.id.courseA9Rl2ImageView1);
        courseA9Rl3ImageView1 = (ImageView) rootView.findViewById(R.id.courseA9Rl3ImageView1);

        titleTicketNumber = (TextView) rootView.findViewById(R.id.titleTicketNumber);
        courseA9TextViewSubmit = (TextView) rootView.findViewById(R.id.courseA9TextViewSubmit);
        courseA9TextViewNext = (TextView) rootView.findViewById(R.id.courseA9TextViewNext);
        courseA9TextViewLast = (TextView) rootView.findViewById(R.id.courseA9Rl3TextViewLast);
        courseA9Rl3TextViewNext = (TextView) rootView.findViewById(R.id.courseA9Rl3TextViewNext);
        courseA9Rl4TextViewLast = (TextView) rootView.findViewById(R.id.courseA9Rl4TextViewLast);

        titleLayoutNumberRl = (RelativeLayout) rootView.findViewById(R.id.titleLayoutNumberRl);
        courseA9Rl1 = (RelativeLayout) rootView.findViewById(R.id.courseA9Rl1);
        courseA9Rl2 = (RelativeLayout) rootView.findViewById(R.id.courseA9Rl2);
        courseA9Rl3 = (RelativeLayout) rootView.findViewById(R.id.courseA9Rl3);
        courseA9Rl4 = (RelativeLayout) rootView.findViewById(R.id.courseA9Rl4);

        courseView = (CourseView) rootView.findViewById(R.id.courseA5CourseView);
        courseView1 = (CourseView) rootView.findViewById(R.id.courseA9Rl3CourseView2);
        courseView2 = (CourseView) rootView.findViewById(R.id.courseA9Rl4CourseView2);
        bottomRecyclerView1 = (RecyclerView) courseA9Rl2Include.findViewById(R.id.bottomRecyclerView);
        bottomAdapter1 = new BottomAdapter(activity, R.layout.coursea5_item_layout, integerList);
        bottomRecyclerView1.setLayoutManager(new GridLayoutManager(activity, integerList.size(), LinearLayoutManager.VERTICAL, false));
        bottomRecyclerView1.setAdapter(bottomAdapter1);
        bottomAdapter1.setOnItemClickInterface(this);


        bottomRecyclerView11 = (RecyclerView) courseA9Rl2Include.findViewById(R.id.bottomRecyclerView1);
        bottomRecyclerView11.setVisibility(View.VISIBLE);
        bottom1Adapter1 = new Bottom1Adapter(activity, R.layout.bottom_item_layout, stringList);
        bottomRecyclerView11.setAdapter(bottom1Adapter1);
        bottomRecyclerView11.setLayoutManager(new GridLayoutManager(activity, stringList.size(), LinearLayoutManager.VERTICAL, false));
        bottom1Adapter1.setOnItemClickInterface(this);

        titleLayoutNextTextView.setOnClickListener(clickAble);
        titleLayoutLastTextView.setOnClickListener(clickAble);
        titleLayoutNumberRl.setOnClickListener(clickAble);
        courseA9FlatSharp.setOnClickListener(clickAble);
        courseA9TimeSignature.setOnClickListener(clickAble);
        courseA9TextViewSubmit.setOnClickListener(clickAble);
        courseA9TextViewNext.setOnClickListener(clickAble);
        courseA9TextViewLast.setOnClickListener(clickAble);

        courseA9Rl3TextViewNext.setOnClickListener(clickAble);
        courseA9Rl4TextViewLast.setOnClickListener(clickAble);

        courseView.setCourseViewListener(this);
        courseView1.setCourseViewListener(this);
        courseView2.setCourseViewListener(this);

        courseView.setTerminate(false);
        courseView1.setTerminate(false);

        courseA9Rl2Include.setVisibility(View.VISIBLE);
        setCourseA9Rl2Visible();
    }

    @SuppressLint("SetTextI18n")
    private void initViewData() {
        if (testModule != null) {
            titleTicketNumber.setText("准考证号：" + "" + testModule.getTickNumber());

            titleLayoutLastTextView.setVisibility(courseIndex == 0 ? View.GONE : View.VISIBLE);
            titleLayoutNextTextView.setVisibility(courseIndex == testModuleList.size() - 1 ? View.GONE : View.VISIBLE);

            titleLayoutSubjectContentTextView.setText((courseIndex + 1) + "丶" + testModule.getSubjectTitle());
            titleLayoutSubjectScoreTextView.setText("（" + testModule.getScore() + "分）");
            titleLayoutNumberTextView.setText("第" + (courseIndex + 1) + "题/第" + (testModuleList.size()) + "题");

            String subjectUrl = testModule.getSubjectUrl();
            if (subjectUrl.contains(",")) {
                Glide.with(activity).
                        load(RetrofitClient.BASE_URL + subjectUrl.substring(0, subjectUrl.indexOf(","))).
                        into(courseA9ImageView1);
                Glide.with(activity).
                        load(RetrofitClient.BASE_URL + subjectUrl.substring(subjectUrl.indexOf(",") + 1, subjectUrl.lastIndexOf(","))).
                        into(courseA9Rl2ImageView1);
                Glide.with(activity).
                        load(RetrofitClient.BASE_URL + subjectUrl.substring(subjectUrl.lastIndexOf(",") + 1)).
                        into(courseA9Rl3ImageView1);
            } else {
                Glide.with(activity).
                        load(RetrofitClient.BASE_URL + testModule.getSubjectUrl()).
                        into(courseA9ImageView1);
            }
        }
    }

    private void showDialog(DialogResultInterface dialogResultInterface) {
        BottomDialog bottomDialog = new BottomDialog(activity, R.style.base_loading_dialogfragment);
        Window window = bottomDialog.getWindow();
        assert window != null;
        WindowManager.LayoutParams p = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();
        window.setGravity(Gravity.LEFT);

        p.x = 0;
        LogUtil.i(TAG, "d.getHeight()=" + d.getHeight() + " y=" + layoutHelper.scaleDimension(100, "y"));
        p.y = (d.getHeight() / 2 - layoutHelper.scaleDimension(200, "y"));
        window.setAttributes(p);
        bottomDialog.setDialogResultInterface(dialogResultInterface);
        bottomDialog.show();
    }

    private DialogResultInterface dialogResultInterface = new DialogResultInterface() {
        @Override
        public void result(String type, String result, String answerId) {
            if (Constant.BOTTOM_RESULT.equals(type)) {
                if (Constant.BOTTOM_DISMISS.equals(result)) {
                    bottomAdapter1.setData(0, new BottomModule(R.drawable.buttom1_down, R.drawable.buttom1_down, true));
                } else if (Constant.BOTTOM_END.equals(result)) {
                    recyclerViewClef = false;
                    courseView.setCurImg(bottomIndex, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView.invalidate();
                    courseView1.setCurImg(bottomIndex, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView1.invalidate();
                    courseView2.setCurImg(bottomIndex, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView2.invalidate();
                } else if (Constant.BOTTOM_START.equals(result)) {
                    recyclerViewClef = true;
                    courseView.setCurImg(bottomIndex, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView.invalidate();
                    courseView1.setCurImg(bottomIndex, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView1.invalidate();
                    courseView2.setCurImg(bottomIndex, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView2.invalidate();
                }
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) courseA9FlatSharp.getLayoutParams();
                if (!recyclerViewClef) {
                    if (riseDropIndex < 7) {
                        layoutParams.topMargin = layoutHelper.scaleDimension(333, "y");
                    } else {
                        layoutParams.topMargin = layoutHelper.scaleDimension(323, "y");
                    }
                } else {
                    if (riseDropIndex < 7) {
                        layoutParams.topMargin = layoutHelper.scaleDimension(317, "y");
                    } else {
                        layoutParams.topMargin = layoutHelper.scaleDimension(310, "y");
                    }
                }
            }
        }
    };

    private void setClick(boolean setCurImg) {
        if (recyclerViewView != null) {
            if (setCurImg) {
                recyclerViewView.setSelected(false);
            }
            if (!recyclerViewView.isSelected()) {
                LogUtil.i(TAG, " click = false");
                recyclerViewClick = false;
                if (setCurImg) {
                    courseView.setCurImg(17, false, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView1.setCurImg(17, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                    courseView2.setCurImg(17, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);

                }
            }
        }
    }

    @Override
    public void onItemClick(BaseViewHolder holder, int position) {
        View viewLayout = holder.getView(R.id.courseA5ItemLayout);
        switch (position) {
            case 0:
                if (viewIndex == 2) {
                    bottomAdapter1.setData(0, new BottomModule(R.drawable.buttom1_up, R.drawable.buttom1_up, true));
                }
                showDialog(dialogResultInterface);
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                this.bottomIndex = position;
                recyclerViewClick = true;
                viewLayout.setSelected(!viewLayout.isSelected());
                if (recyclerViewView != null && recyclerViewView.getX() != viewLayout.getX()) {
                    recyclerViewView.setSelected(false);
                } else {
                    setClick(false);
                }
                recyclerViewView = viewLayout;
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                if (recyclerViewMark != position) {
                    recyclerViewMark = position;
                } else {
                    recyclerViewMark = 0;
                }
                viewLayout.setSelected(!viewLayout.isSelected());
                if (recyclerViewView1 != null && recyclerViewView1.getX() != viewLayout.getX()) {
                    recyclerViewView1.setSelected(false);
                }
                recyclerViewView1 = viewLayout;
                break;
            case 11:
                recyclerViewSpot = !recyclerViewSpot;
                viewLayout.setSelected(!viewLayout.isSelected());
                recyclerViewView2 = viewLayout;
                break;
            case 13:
                recyclerViewThreeNote = !recyclerViewThreeNote;
                viewLayout.setSelected(!viewLayout.isSelected());
                recyclerViewView4 = viewLayout;
                break;
            case 14:
                break;
            case 15:
                recyclerViewHomoPhonicConnection = !recyclerViewHomoPhonicConnection;
                viewLayout.setSelected(!viewLayout.isSelected());
                recyclerViewView3 = viewLayout;
                break;
            case 16:
                courseView.setCurImg(position, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                courseView1.setCurImg(position, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                courseView2.setCurImg(position, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                break;
            case 12:
                viewLayout.setSelected(!viewLayout.isSelected());
                bottomIndex = -1;
                if (recyclerViewView != null) {
                    recyclerViewView.setSelected(false);
                }
                if (!recyclerViewIsRepose) {
                    recyclerViewIsRepose = true;
                    for (int i = 0; i < resId.getReposeId().length; i++) {
                        bottomAdapter1.setData(i + 1, new BottomModule(resId.getReposeId()[i], resId.getReposeId()[i], true));
                    }
                } else {
                    recyclerViewIsRepose = false;
                    for (int i = 0; i < resId.getReposeId().length; i++) {
                        bottomAdapter1.setData(i + 1, new BottomModule(resId.getImageId()[i + 1], resId.getImageId()[i + 1], true));
                    }
                }
                break;
            default:
                viewLayout.setSelected(!viewLayout.isSelected());
                break;
        }
        if (position != 16) {
            courseView.setCurImg(position, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
            courseView1.setCurImg(position, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
            courseView2.setCurImg(position, recyclerViewClick, recyclerViewIsRepose, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
            if (position == 14 || (recyclerViewIsRepose && (position == 1 || position == 2 || position == 3 || position == 4 || position == 5))) {
                if (viewIndex == 2) {
                    courseView.setBitmap(position);
                } else if (viewIndex == 3) {
                    courseView1.setBitmap(position);
                } else if (viewIndex == 4) {
                    courseView2.setBitmap(position);
                }
            }
        }
    }

    @Override
    public void onItemBottomClick(BaseViewHolder holder, int position) {
        View viewLayout = holder.getView(R.id.bottomItemLayoutTextView);
        switch (position) {
            case 0:
                if (viewIndex == 2) {
                    courseView.setHighLow(7);
                } else if (viewIndex == 3) {
                    courseView1.setHighLow(7);
                } else if (viewIndex == 4) {
                    courseView2.setHighLow(7);
                }
                break;
            case 8:
                if (viewIndex == 2) {
                    courseView.setHighLow(-7);
                } else if (viewIndex == 3) {
                    courseView1.setHighLow(-7);
                } else if (viewIndex == 4) {
                    courseView2.setHighLow(-7);
                }

                break;
            default:
                if (!recyclerViewIsRepose) {
                    if (bottomIndex != -1) {
                        if (viewIndex == 2) {
                            courseView.setCurImg(bottomIndex, recyclerViewClick, false, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                            if (recyclerViewClick)
                                courseView.setBitmap(position);
                        } else if (viewIndex == 3) {
                            courseView1.setCurImg(bottomIndex, recyclerViewClick, false, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                            if (recyclerViewClick)
                                courseView1.setBitmap(position);
                        } else if (viewIndex == 4) {
                            courseView2.setCurImg(bottomIndex, recyclerViewClick, false, recyclerViewSpot, recyclerViewMark, recyclerViewHomoPhonicConnection, recyclerViewThreeNote, recyclerViewClef);
                            if (recyclerViewClick)
                                courseView2.setBitmap(position);
                        }
                    }
                }
                break;
        }
    }

    @SuppressLint("RtlHardcoded")
    private void showFlatSharpDialog(final ImageView sharpView, boolean type) {
        FlatSharpDialog flatSharpDialog = new FlatSharpDialog(activity, R.style.base_loading_dialogfragment, type);
        Window window = flatSharpDialog.getWindow();
        assert window != null;
        WindowManager.LayoutParams p = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        int[] location = new int[2];
        sharpView.getLocationOnScreen(location);
        LogUtil.i(TAG, " logcation[0]=" + location[0] + " location[1]=" + location[1]);
        // 新位置X坐标
        p.x = location[0] - sharpView.getWidth() / 2;
        // 新位置Y坐标
        p.y = location[1];
        window.setAttributes(p);
        flatSharpDialog.show();
        flatSharpDialog.setDialogResultInterface(new DialogResultInterface() {
            @Override
            public void result(String type, String result, String answerId) {
                riseDropIndex = Integer.valueOf(result);
                if (Constant.COURSE_A9_DIALOG_RESULT1.equals(type)) {
                    int index = Integer.valueOf(result);

                    sharpView.setImageResource(resId.getFateId()[riseDropIndex]);
                    courseView.setFlatIndex(riseDropIndex);
                    courseView1.setFlatIndex(riseDropIndex);
                    courseView2.setFlatIndex(riseDropIndex);

                    for (int i = 0; i < stringList.size(); i++) {
                        stringList.set(i, stringList.get(i).replace("b", ""));
                        stringList.set(i, stringList.get(i).replace("#", ""));

                        stringList1.set(i, stringList1.get(i).replace("b", ""));
                        stringList1.set(i, stringList1.get(i).replace("#", ""));
                    }
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) sharpView.getLayoutParams();
                    if (index < 7) {
                        for (int i = 0; i < riseDropIndex + 1; i++) {
                            stringList.set(resId.getHighDropIndexId()[i], "b" + stringList.get(resId.getHighDropIndexId()[i]));
                            stringList1.set(resId.getHighDropIndexId()[i], "b" + stringList1.get(resId.getHighDropIndexId()[i]));
                        }
                        bottom1Adapter1.notifyDataSetChanged();
                    } else {
                        for (int i = 0; i < riseDropIndex - 6; i++) {
                            stringList.set(resId.getHighRiseIndexId()[i], "#" + stringList.get(resId.getHighRiseIndexId()[i]));
                            stringList1.set(resId.getHighRiseIndexId()[i], "#" + stringList1.get(resId.getHighRiseIndexId()[i]));
                        }
                        bottom1Adapter1.notifyDataSetChanged();
                    }
                    if (!recyclerViewClef) {
                        if (riseDropIndex < 7) {
                            layoutParams.topMargin = layoutHelper.scaleDimension(333, "y");
                        } else {
                            layoutParams.topMargin = layoutHelper.scaleDimension(323, "y");
                        }
                    } else {
                        if (riseDropIndex < 7) {
                            layoutParams.topMargin = layoutHelper.scaleDimension(317, "y");
                        } else {
                            layoutParams.topMargin = layoutHelper.scaleDimension(310, "y");
                        }
                    }
                } else if (Constant.COURSE_A9_DIALOG_RESULT2.equals(type)) {
                    sharpView.setImageResource(resId.getTimeImageId()[Integer.valueOf(result)]);

                    if (riseDropIndex == 0 || riseDropIndex == 1 || riseDropIndex == 3) {
                        courseView.setNoteType(4);
                        courseView.setLineType(true);

                        courseView1.setNoteType(4);
                        courseView1.setLineType(true);
                    } else {
                        courseView.setNoteType(6);
                        courseView.setLineType(true);

                        courseView1.setNoteType(6);
                        courseView1.setLineType(true);
                    }

                }
            }
        });
    }

    private void setCourseA9Rl1Visible() {
        courseA9Rl1.setVisibility(View.VISIBLE);
        courseA9Rl2.setVisibility(View.GONE);
        courseA9Rl3.setVisibility(View.GONE);
        courseA9Rl4.setVisibility(View.GONE);
    }

    private void setCourseA9Rl2Visible() {
        viewIndex = 2;
        courseA9Rl1.setVisibility(View.GONE);
        courseA9Rl2.setVisibility(View.VISIBLE);
        courseA9Rl3.setVisibility(View.GONE);
        courseA9FlatSharp.setVisibility(View.VISIBLE);
        courseA9Rl4.setVisibility(View.GONE);
        courseA9TimeSignature.setVisibility(View.VISIBLE);
    }


    private void setCourseA9Rl3Visible() {
        viewIndex = 3;
        courseA9Rl1.setVisibility(View.GONE);
        courseA9Rl2.setVisibility(View.GONE);
        courseA9Rl3.setVisibility(View.VISIBLE);
        courseA9Rl4.setVisibility(View.GONE);
        courseA9TimeSignature.setVisibility(View.GONE);
    }

    private void setCourseA9Rl4Visible() {
        viewIndex = 4;
        courseA9Rl1.setVisibility(View.GONE);
        courseA9Rl2.setVisibility(View.GONE);
        courseA9Rl3.setVisibility(View.GONE);
        courseA9Rl4.setVisibility(View.VISIBLE);
        courseA9TimeSignature.setVisibility(View.GONE);
    }

    @Override
    public void courseViewResult(int type) {
        if (1 == type) {
            recyclerViewMark = 0;
            if (recyclerViewView1 != null) {
                recyclerViewView1.setSelected(false);
            }
            if (recyclerViewView2 != null) {
                recyclerViewSpot = false;
                recyclerViewView2.setSelected(false);
            }
            LogUtil.i(TAG, "courseViewResult");
        }
        if (2 == type) {
            recyclerViewThreeNote = false;
            LogUtil.i(TAG, "222222222222222222222222222222222");
            if (recyclerViewView4 != null) {
                recyclerViewView4.setSelected(false);
            }
        }
        if (3 == type) {
            LogUtil.i(TAG, "33333333333333333333333333333333333");
            recyclerViewHomoPhonicConnection = false;
            if (recyclerViewView3 != null) {
                recyclerViewView3.setSelected(false);
            }
        }
        if (4 == type) {
            if (recyclerViewView != null) {
                recyclerViewView.setSelected(false);
                recyclerViewClick = false;
                setClick(true);
            }
        }
    }


    private class ClickAble implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.courseA9TextViewNext:
                    setCourseA9Rl3Visible();
                    break;
                case R.id.courseA9Rl3TextViewLast:
                    setCourseA9Rl2Visible();
                    break;
                case R.id.courseA9TextViewSubmit:
                    courseA9Rl2Include.setVisibility(View.VISIBLE);
                    setCourseA9Rl2Visible();
                    break;
                case R.id.courseA9Rl3TextViewNext:
                    setCourseA9Rl4Visible();
                    break;
                case R.id.courseA9Rl4TextViewLast:
                    setCourseA9Rl3Visible();
                    break;
                case R.id.courseA9FlatSharp:
                    LogUtil.i(TAG, "courseA9FlatSharp");
                    showFlatSharpDialog(courseA9FlatSharp, false);
                    break;
                case R.id.courseA9TimeSignature:
                    LogUtil.i(TAG, "courseA9TimeSignature");
                    showFlatSharpDialog(courseA9TimeSignature, true);
                    break;
                case R.id.titleLayoutNextTextView:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    courseInterface.courseInterfaceResult(Constant.NEXT_KNOWLEDGE, courseIndex + 1);
                    break;
                case R.id.titleLayoutLastTextView:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    courseInterface.courseInterfaceResult(Constant.LAST_KNOWLEDGE, courseIndex - 1);
                    break;
                case R.id.titleLayoutNumberRl:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    dialogShowListener.dialogShow(0, courseIndex, result());
                    break;
                default:
                    break;
            }
        }
    }

    private boolean result() {
        getScore();

        Constant.getScreen(activity, SPUtil.getSharedPreferencesData(activity, "number") + "_" + (courseIndex + 1),SPUtil.getSharedPreferencesData(activity, "number")+"_"+ (courseIndex + 1));
        return courseView.getBitMapModuleList().size() > 0 || courseView1.getBitMapModuleList().size() > 0 || courseView2.getBitMapModuleList().size() > 0 || riseDropIndex != -1;
    }

    private void getScore() {
        List<AnswerXml> list = answerModuleList.get(0).getAnswerXmlList();
        if (courseView == null) return;
        List<AnswerXml> answerXmlList = courseView.getAnswerXmlList();
        if (courseView1 != null) {
            answerXmlList.addAll(courseView1.getAnswerXmlList());
        }
        if (courseView2 != null) {
            answerXmlList.addAll(courseView2.getAnswerXmlList());
        }
        for (int i = 0; i < answerXmlList.size(); i++) {
            if (i < list.size()) {
                List<Note> noteList = list.get(i).getNoteList();
                for (int j = 0; j < noteList.size(); j++) {
                    Note note = noteList.get(j);
                    List<Note> noteLists = answerXmlList.get(i).getNoteList();
                    if (j < noteLists.size()) {
                        if (note.getNormalType() != null || note.getTied() != null) {
                            if (note.getNormalType() != null) {
                                if (j + 2 < noteList.size() && j + 2 < noteLists.size()) {
                                    if (noteList.get(j + 2).equalsNote(noteLists.get(j + 2), true, false)) {
                                        score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                    }
                                    if (noteList.get(j + 1).equalsNote(noteLists.get(j + 1), true, false)) {
                                        score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                    }
                                    if (noteList.get(j).equalsNote(noteLists.get(j), true, false)) {
                                        score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                    }
                                    j += 2;
                                } else {
                                    if (j + 1 < noteList.size() && j + 1 < noteLists.size()) {
                                        if (noteList.get(j + 1).equalsNote(noteLists.get(j + 1), true, false)) {
                                            score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                        }
                                        if (noteList.get(j).equalsNote(noteLists.get(j), true, false)) {
                                            score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                        }
                                        j += 1;
                                    } else {
                                        if (j < noteList.size() && j < noteLists.size()) {
                                            if (noteList.get(j).equalsNote(noteLists.get(j), true, false)) {
                                                score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                            }
                                        }
                                    }
                                }
                            } else {
                                if (j + 1 < noteList.size() && j + 1 < noteLists.size()) {
                                    if (noteList.get(j + 1).equalsNote(noteLists.get(j + 1), false, true)) {
                                        score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                    }
                                    if (j < noteList.size() && j < noteLists.size()) {
                                        if (note.equalsNote(noteLists.get(j), false, true)) {
                                            score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                        }
                                    }
                                    j += 1;
                                } else {
                                    if (j < noteList.size() && j < noteLists.size()) {
                                        if (note.equalsNote(noteLists.get(j), false, true)) {
                                            score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                                        }
                                    }
                                }

                            }
                        } else {
                            if (note.equalsNote(noteLists.get(j), false, false)) {
                                score += Double.valueOf(answerModuleList.get(0).getScore()) / noteList.size();
                            }
                        }
                    }
                }
            }
        }
        SPUtil.setSharedPreferencesData(activity.getApplication(), CourseA9.class.getSimpleName(), score + "");
    }

    @Override
    public void setCourseInterface(CourseInterface courseInterface) {
        this.courseInterface = courseInterface;
    }

    @Override
    public void setCourseDialogListener(DialogShowListener dialogListener) {
        this.dialogShowListener = dialogListener;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void show(List<TestModule> courseEntities, int index, String type) {
        initViewData();
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }
}
